Skip to content

BUG: fix fill value for gouped sum in case of unobserved categories for string dtype (empty string instead of 0) #61909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jorisvandenbossche
Copy link
Member

I ran into one more case of the sum of empty / all-NaN to use "0" instead of empty string (#60229), specifically when effectively introducing empty groups with categorical data with observed=False.

Follow-up on #60936

The passing through of is_string through several layers is a bit annoying, but effectively is needed to for now only changes this for string dtype, and not for object dtype in general (which in the other PR related to this, we did for now)

@jorisvandenbossche jorisvandenbossche added this to the 2.3.2 milestone Jul 19, 2025
@jorisvandenbossche jorisvandenbossche added Bug Groupby Strings String extension data type and string data labels Jul 19, 2025
@@ -729,6 +730,10 @@ def group_sum(
sumx = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
compensation = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)

if is_string:
# for strings start with empty string instead of 0 as `initial` value
sumx = np.full((<object>out).shape, "", dtype=object)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would passing “initial” be more general/idiomatic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially thinking that as well, but then because this would in practice only be used for the specific case of strings, I thought to be more explicit about that fact. But in both cases I have to pass it down a few layers, so whether it is then called initial or is_string actually does not matter much, and initial at least makes it clearer where it is called from the EA _groupby_ops what the purpose is of passing it down.
So updated to use initial in the last commit

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jorisvandenbossche jorisvandenbossche merged commit 27928ed into pandas-dev:main Jul 22, 2025
43 checks passed
@jorisvandenbossche jorisvandenbossche deleted the string-dtype-groupby-sum-observed-false-fillvalue branch July 22, 2025 07:20
Copy link

lumberbot-app bot commented Jul 22, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.3.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 27928edc61f5b01e933036a99549636425e5a557
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #61909: BUG: fix fill value for gouped sum in case of unobserved categories for string dtype (empty string instead of 0)'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-61909-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #61909 on branch 2.3.x (BUG: fix fill value for gouped sum in case of unobserved categories for string dtype (empty string instead of 0))"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Still Needs Manual Backport Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants